import {
  toast
} from "../../utils/util";

// personal/fans/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      all: {
        name: '累计获得',
        num: 0
      },
      used: {
        name: '已使用',
        num: 0
      },
      canUse: {
        name: '当前可用',
        num: 0
      }
    },
    fansList: [],
    cansendcount: 0,
    havesendcount: 0,
    fans: 0,
    iDisplayStart: 0,
    iDisplayLengthQUERY: 10,
    allFans: 0,
    userLevel: 0,
    remainder: 0,
    total: 0,
    indirectFansList: [],
    indirectTotal: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initInfo(options)
    this.getFans()
    this.getDirectFans()
  },
  initInfo(options) {
    const allFans = options.num
    const {
      cansendcount,
      havesendcount,
      userlevel,
      remainder
    } = wx.getStorageSync('userInfo')
    const all = 'info.all.num'
    const used = 'info.used.num'
    const canUse = 'info.canUse.num'
    this.setData({
      [all]: cansendcount + havesendcount,
      [used]: havesendcount,
      [canUse]: cansendcount,
      allFans,
      userLevel: userlevel,
      remainder
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  getFans() {
    const code = 'recommendUserCount'
    app.$api.common.commonFindName({
        code
      })
      .then((res) => {
        const data = res.data
        this.setData({
          fans: data.name
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  getDirectFans() {
    const {
      fansList,
      indirectFansList
    } = this.data
    const {
      iDisplayStart,
      iDisplayLengthQUERY
    } = this.data
    app.$api.user.recommend({
        iDisplayStart,
        iDisplayLengthQUERY
      })
      .then((res) => {
        const data = res.data
        const result = fansList.concat(data.directData)
        this.setData({
          fansList: result,
          total: data.directCount,
          indirectTotal: data.inDirectCount,
          indirectFansList: data.inDirectData
        })
        console.log(fansList)
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {
      fansList,
      total
    } = this.data
    this.setData({
      iDisplayStart: fansList.length
    })
    if (fansList.length === total) {
      toast('没有更多', 1000)
      return false
    }
    this.getDirectFans()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindVip() {
    wx.navigateTo({
      url: '../../vip/upgrade/index'
    })
  }
})